<template>
    <a-modal
      :bodyStyle="bodyStyle"
      :title="title"
      :visible="visible"
      :confirmLoading="confirmLoading"
      :destroyOnClose="true"
      :maskClosable="false"
      @ok="handleOk"
      @cancel="handleCancel"
    >
      <a-form layout='vertical' :form="form">
        <a-form-item  class="a-form-item" v-bind="formItemLayout" label="应用ID">
          <a-input
            v-decorator="[
              'imageSearchAppId',
              {
                rules: [{ required: true, message: '请输入以图搜图应用ID!' }],
              }
            ]"
            placeholder="请输入以图搜图应用ID"
          />
        </a-form-item>
        <a-form-item  class="a-form-item" v-bind="formItemLayout" label="apiKey">
          <a-input
            v-decorator="[
              'imageSearchApiKey',
              {
                rules: [{ required: true, message: '请输入apiKey!' }],
              }
            ]"
            placeholder="请输入apiKey"
          />
        </a-form-item>
        <a-form-item  class="a-form-item" v-bind="formItemLayout" label="secretKey">
          <a-input
            v-decorator="[
              'imageSearchSecretKey',
              {
                rules: [{ required: true, message: '请输入secretKey!' }],
              }
            ]"
            placeholder="请输入secretKey"
          />
        </a-form-item>
        <a-form-item class="a-form-item" v-bind="formItemLayout" label="图库用途">
          <a-radio-group v-decorator="['galleryUse',
              {
                rules: [{ required: true, message: '请选择图库用途!' }],
              }]">
            <a-radio :value="1">
              产品图库
            </a-radio>
            <a-radio :value="2">
              备选图库
            </a-radio>
            <a-radio :value="3">
              素材图库
            </a-radio>
          </a-radio-group>
        </a-form-item>
        <a-form-item  class="a-form-item" v-bind="formItemLayout" label="最大分配客户数">
          <a-input-number
            :min="1"
            :max="60000"
            :precision="0"
            v-decorator="[
              'maxConnections',
              {
                rules: [{ required: true, message: '请输入图库最大可分配客户数量!' }],
              }
            ]"
            placeholder="请输入图库最大可分配客户数量"
          />
        </a-form-item>
        <a-form-item class="form-item-hidden">
          <a-input type="hidden" v-decorator="['id', {}]"/>
        </a-form-item>
      </a-form>
    </a-modal>
</template>
<script>
import Vue from 'vue'
import serv from './ImageSearchConfigService'
import { Form, Input, Modal, Radio, InputNumber } from 'ant-design-vue'

Vue.component(Radio.name, Radio)
Vue.component(Radio.Group.name, Radio.Group)
Vue.component(Form.name, Form)
Vue.component(Form.Item.name, Form.Item)
Vue.component(Input.name, Input)
Vue.component(InputNumber.name, InputNumber)
Vue.component(Modal.name, Modal)
export default {
  beforeCreate () {
    this.form = this.$form.createForm(this)
  },
  created () {

  },
  data () {
    return {
      confirmLoading: false,
      formItemLayout: {
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 }
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 18 }
        }
      },
      bodyStyle: { height: 400 },
      loading: false
    }
  },
  props: {
    title: {
      type: String,
      required: true
    },
    visible: {
      type: Boolean,
      default: false
    },
    record: {
      type: Object,
      default () {
        return {}
      }
    }
  },
  watch: {
    visible: function (nvalue) {
      if (nvalue && Object.keys(this.record).length > 0) {
        setTimeout(() => {
          this.form.setFieldsValue(this.record)
        }, 10)
      }
    }
  },
  methods: {
    handleOk () {
      const form = this.form
      form.validateFields((err, values) => {
        if (err) {
          return
        }

        this.confirmLoading = true
        serv.save(values).then(res => {
          this.confirmLoading = false
          if (res.data.code === 0) {
            form.resetFields()
            this.$emit('ok', values.id)
          } else {
            this.$message.error(res.data.msg)
          }
        })
      })
    },
    handleCancel (e) {
      this.$emit('cancel')
    }
  }
}
</script>

<style scoped>
img {
  max-width: 100%;
}

.a-form-item {
  margin-bottom: 0px;
}
</style>
